# Author: Mark Richardson
# Purpose: Compare hierarchical and non-hierarchical performance ratings

# Load packages
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(patchwork)

# Load data
load("data/07_performance_ratings_all_fmtd.RData")

# Create plot index
perf_ratings <- perf_ratings %>%
  arrange(perf_rating_inf_hier) %>%
  mutate(plot_index = 1:n())

# Get correlation of ratings
format(cor(perf_ratings$perf_rating_inf_hier,
           perf_ratings$perf_rating_inf), digits = 2)

# Get correlations of standard deviations
format(cor(perf_ratings$perf_rating_inf_hier_sd,
           perf_ratings$perf_rating_inf_sd), digits = 2)

# Get pct. of smaller std. devs.
format(mean(perf_ratings$perf_rating_inf_hier_sd <
              perf_ratings$perf_rating_inf_sd) * 100, digits = 2)

# Create Figure B1

r_min <- min(c(perf_ratings$perf_rating_inf_hier, perf_ratings$perf_rating_nv_hier,
               perf_ratings$perf_rating_inf, perf_ratings$perf_rating_nv)) - 0.5

r_max <- max(c(perf_ratings$perf_rating_inf_hier, perf_ratings$perf_rating_nv_hier,
               perf_ratings$perf_rating_inf, perf_ratings$perf_rating_nv)) + 0.5

mn_inf <- ggplot(perf_ratings) +
  geom_point(aes(perf_rating_inf, perf_rating_inf_hier), alpha = 0.5, size = 2) +
  labs(title = "Informed Models",
       x = "Posterior Mean (Non-Hierarchical)",
       y = "Posterior Mean (Hierarchical)",
       caption = "Plot includes a 45-degree line.") +
  geom_abline(intercept = 0, slope = 1) +
  xlim(r_min, r_max) +
  ylim(r_min, r_max) +
  theme_bw()

sd_n_inf <- ggplot(perf_ratings) +
  geom_point(aes(perf_rating_inf_sd, perf_rating_inf_hier_sd, 
                 fill = n_inf_perf_ratings <= 5), shape = 21) +
  geom_abline(intercept = 0, slope = 1) +
  labs(title = "Informed Models",
       x = "Posterior Standard Deviation (Non-Hierarchical)",
       y = "Posterior Standard Deviation (Hierarchical)",
       fill = expression(n_inf <= 5)) +
  xlim(0, 1) +
  ylim(0, 1) +
  scale_fill_manual(values = alpha(c("black", "white"), .5)) +
  theme_bw() +
  theme(legend.position = "bottom")

mn_inf / sd_n_inf

ggsave("figs/fgB1.png",
       device = "png", dpi = "retina",
       units = "in",  width = 5, height = 10)
